layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;
	
	var num;
	//加载页面数据
	var newsData = '';
	var url = "/xjeca/adminnews_findByCategory";
	var param={"categoryId":3};
	$.get(url,param, function(data){
		//数据渲染
       	newsList(data);
	});
	
	$("#search").click(function(){
		var num = $("#categoryId").val();
		var name = $("#title").val();
		$.get("/xjeca/adminnews_findByCategory?news_title="+name+"&categoryId="+num,function(data){
			newsList(data);
		});
	});
	
	$("#aaa").click(function(){
		var num = $("#categoryId").val();
		$.get("/xjeca/adminnews_findByCategory?categoryId="+num,function(data){
			newsList(data);
		});
	});
	
	//查询
	$(".search_btn").click(function(){
		var newArray = [];
		if($(".search_input").val() != ''){
			var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
            setTimeout(function(){
            	$.ajax({
					url : "../../json/newsList.json",
					type : "get",
					dataType : "json",
					success : function(data){
						if(window.sessionStorage.getItem("addNews")){
							var addNews = window.sessionStorage.getItem("addNews");
							newsData = JSON.parse(addNews).concat(data);
						}else{
							newsData = data;
						}
						for(var i=0;i<newsData.length;i++){
							var newsStr = newsData[i];
							var selectStr = $(".search_input").val();
		            		function changeStr(data){
		            			var dataStr = '';
		            			var showNum = data.split(eval("/"+selectStr+"/ig")).length - 1;
		            			if(showNum > 1){
									for (var j=0;j<showNum;j++) {
		            					dataStr += data.split(eval("/"+selectStr+"/ig"))[j] + "<i style='color:#03c339;font-weight:bold;'>" + selectStr + "</i>";
		            				}
		            				dataStr += data.split(eval("/"+selectStr+"/ig"))[showNum];
		            				return dataStr;
		            			}else{
		            				dataStr = data.split(eval("/"+selectStr+"/ig"))[0] + "<i style='color:#03c339;font-weight:bold;'>" + selectStr + "</i>" + data.split(eval("/"+selectStr+"/ig"))[1];
		            				return dataStr;
		            			}
		            		}
		            		//文章标题
		            		if(newsStr.newsName.indexOf(selectStr) > -1){
			            		newsStr["newsName"] = changeStr(newsStr.newsName);
		            		}
		            		//发布人
		            		if(newsStr.newsAuthor.indexOf(selectStr) > -1){
			            		newsStr["newsAuthor"] = changeStr(newsStr.newsAuthor);
		            		}
		            		//浏览权限
		            		if(newsStr.newsLook.indexOf(selectStr) > -1){
			            		newsStr["newsLook"] = changeStr(newsStr.newsLook);
		            		}
		            		//发布时间
		            		if(newsStr.newsTime.indexOf(selectStr) > -1){
			            		newsStr["newsTime"] = changeStr(newsStr.newsTime);
		            		}
		            		if(newsStr.newsName.indexOf(selectStr)>-1 || newsStr.newsAuthor.indexOf(selectStr)>-1 || newsStr.newsStatus.indexOf(selectStr)>-1 || newsStr.newsLook.indexOf(selectStr)>-1 || newsStr.newsTime.indexOf(selectStr)>-1){
		            			newArray.push(newsStr);
		            		}
		            	}
		            	newsData = newArray;
		            	newsList(newsData);
					}
				})
            	
                layer.close(index);
            },2000);
		}else{
			layer.msg("请输入需要查询的内容");
		}
	})

	//添加文章
	//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	$(window).one("resize",function(){
		$(".newsAdd_btn").click(function(){
			var index = layui.layer.open({
				title : "添加文章",
				type : 2,
				content : "newsAdd.jsp",
				success : function(layero, index){
					setTimeout(function(){
						layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
					},500)
				}
			})			
			layui.layer.full(index);
		})
	}).resize();

	

	//批量删除
	$(".batchDel").click(function(){
		var $checkbox = $('.news_list tbody input[type="checkbox"][name="checked"]');
		var $checked = $('.news_list tbody input[type="checkbox"][name="checked"]:checked');
		if($checkbox.is(":checked")){
			layer.confirm('确定删除选中的信息？',{icon:3, title:'提示信息'},function(index){
				var index = layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
	            setTimeout(function(){
	            	//删除数据
	            	for(var j=0;j<$checked.length;j++){
	            		for(var i=0;i<newsData.length;i++){
							if(newsData[i].news_id == $checked.eq(j).parents("tr").find(".news_del").attr("data-id")){
								/*newsData.splice(i,1);
								newsList(newsData);*/
								$checked.eq(j).parents("tr").remove();
								$.post("/xjeca/adminnews_del?news_id="+newsData[i].news_id,function(){
									
								},"json");
								
							}
						}
	            	}
	            	$('.news_list thead input[type="checkbox"]').prop("checked",false);
	            	form.render();
	                layer.close(index);
					layer.msg("删除成功");
	            },2000);
	        })
		}else{
			layer.msg("请选择需要删除的文章");
		}
	})

	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})

	//是否展示
	form.on('switch(isShow)', function(data){
		var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
			layer.msg("展示状态修改成功！");
        },300);
        function sleep(numberMillis) {
		    var now = new Date();
		    var exitTime = now.getTime() + numberMillis;
		    while (true) {
		        now = new Date();
		        if (now.getTime() > exitTime)
		            return;
		    }
		}
        sleep(301);  //睡眠0.3秒
        var _this = $(this);
       window.location.href="adminnews_editState?news_id="+_this.attr("data-id")+"&news_isshow="+_this.attr("value");
       /*$.post("/xjeca/adminnews_editState?news_id="+_this.attr("data-id")+"&news_isshow="+_this.attr("value"),function(){
       },"json");*/
       
	})
 
	//操作
	$("body").on("click",".news_edit",function(){  //编辑
		/*layer.alert('您点击了文章编辑按钮，由于是纯静态页面，所以暂时不存在编辑内容，后期会添加，敬请谅解。。。',{icon:6, title:'文章编辑'});*/
		var _this = $(this);
		window.location.href="adminnews_updateById?news_id="+_this.attr("data-id");
	})
	
	$("body").on("click",".news_del",function(){  //删除
		var _this = $(this);
		layer.confirm('确定删除此信息？',{icon:3, title:'提示信息'},function(index){
					//异步删除news
					$.post("/xjeca/adminnews_del?news_id="+_this.attr("data-id"),function(){
						
					},"json");
				_this.parents("tr").remove();
			layer.close(index);
		});
	})
	
	function newsList(that){
		//渲染数据
		function renderDate(data,curr){
			var dataHtml = '';
			if(!that){
				currData = newsData.concat().splice(curr*nums-nums, nums);
			}else{
				currData = that.concat().splice(curr*nums-nums, nums);
			}
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+currData[i].news_title+'</td>'
			    	+'<td>'+(currData[i].news_author==undefined ? '':currData[i].news_author)+'</td>';
			    	dataHtml += /*'<td>'+(currData[i].news_perm=="1"?"会员浏览":"公开浏览")+'</td>'
			    	+*/'<td><input type="checkbox" name="news_isshow" data-id="'+currData[i].news_id+'"  lay-skin="switch"  lay-text="是|否" lay-filter="isShow"'+(currData[i].news_isshow=='1'?"checked value='1'":"value='0'")+'></td>'
		    	+'<td>'+(currData[i].news_time==undefined ? '':currData[i].news_time)+'</td>'
			    	+'<td>'
				+  '<a class="layui-btn layui-btn-mini news_edit" data-id="'+currData[i].news_id+'"><i class="iconfont icon-edit"></i> 编辑</a>'
				+  '<a class="layui-btn layui-btn-danger layui-btn-mini news_del" data-id="'+currData[i].news_id+'"><i class="layui-icon">&#xe640;</i> 删除</a>'
		        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
			}
		    return dataHtml;
		}
		
		//分页
		var nums = 8; //每页出现的数据量
		if(that){
			newsData = that;
		}
		laypage({
			cont : "page",
			pages : Math.ceil(newsData.length/nums),
			jump : function(obj){
				$(".news_content").html(renderDate(newsData,obj.curr));
				$('.news_list thead input[type="checkbox"]').prop("checked",false);
		    	form.render();
			}
		})
	}
})
